﻿using System;
using System.Collections.Generic;
using System.Text;
using Studio.Network.Utilities;
using System.Drawing;
using Studio.Network.Kaixin001.Entities;

namespace Studio.Network.Kaixin001
{
    public class KXInterface
    {
        public static readonly string CACHE_FOLDER = "cache";

        public static readonly string HOST = "www.kaixin001.com";

        public static string[] KAIXIN001_SERVERS = new string[] {             
            //    
            "218.240.13.69", "218.240.13.70", "218.240.13.71", "218.240.13.72",
            //
            "119.161.132.25", "119.161.132.26", "119.161.132.27", "119.161.132.28",  "119.161.132.29", "119.161.132.30",
            //
            "123.103.99.50", "123.103.99.51", 
            //
            "123.103.99.80", "123.103.99.81",
            //
            "123.103.99.132", "123.103.99.133", "123.103.99.134", 
            //
            "123.103.99.182", "123.103.99.183", "123.103.99.184", "123.103.99.185", 
            //    
            "123.103.101.95", "123.103.101.96", "123.103.101.97", "123.103.101.98", "123.103.101.99",
            //
            "123.103.102.127", "123.103.102.128", "123.103.102.129", "123.103.102.130", "123.103.102.131"
        };


        public static string[] MAP_NAME = { "未验证(网络错误)", "合法", "真名非法", "真名违禁" };
        public static string[] MAP_MAIL = { "未验证(网络错误)", "邮箱可用", "邮箱已注册但尚未通过验证", "邮箱注册已验证" };
        public static string[] MAP_CODE = { "未验证(网络错误)", "非法", "合法" };

        public static readonly string URL_KX_BASE = "http://www.kaixin001.com/";
        public static readonly string URL_KX_HOME = "http://www.kaixin001.com/home/";
        public static readonly string URL_KX_APP = "http://www.kaixin001.com/app/app.php?aid={0}";

        public static readonly string URL_KX_NAME_CHECK = "http://www.kaixin001.com/interface/checkname.php?mode=detail&real_name={0}";
        public static readonly string URL_KX_MAIL_CHECK = "http://www.kaixin001.com/interface/checkemail.php?email={0}";
        public static readonly string URL_KX_CODE_CHECK = "http://www.kaixin001.com/interface/checkcode.php?code={0}&rcode={1}";

        public static readonly string URL_KX_CODE = "http://www.kaixin001.com/interface/regcreatepng.php?randnum={0}";

        public static readonly string URL_KX_CODE_RAND_0 = "http://www.kaixin001.com/interface/regcreatepng.php?randnum=0";

        public static readonly string URL_LOGIN_PAGE = "http://www.kaixin001.com/login/login.php";
        public static readonly string URL_LOGIN = "http://www.kaixin001.com/login/login.php?email={0}&password={1}";
        

        // public static string RAND = "0.17098241767545763";
        // public static string CODE = "Aekz";

        public static readonly string RAND = "0";
        public static readonly string CODE = "BTdv";

        public static int CheckName(string realname)
        {
            string response = new HttpHelper().POST(string.Format(KXInterface.URL_KX_NAME_CHECK, realname));
            int r;
            return int.TryParse(response, out r) ? r : -1;
        }

        public static int CheckMail(string email)
        {
            string response = new HttpHelper().POST(string.Format(KXInterface.URL_KX_MAIL_CHECK, email));
            int r;
            return int.TryParse(response, out r) ? r : -1;
        }

        public static int CheckCode(string code, string rcode)
        {
            string response = new HttpHelper().POST(string.Format(KXInterface.URL_KX_CODE_CHECK, code, rcode));
            int r;
            return int.TryParse(response, out r) ? r : -1;
        }

        public static Image LoadKxCode()
        {
            HttpHelper http = new HttpHelper(Encoding.ASCII, Encoding.UTF8);
            http.Timeout = 3000;
            return http.GetImage(URL_KX_CODE_RAND_0);
        }


        public static string _verify = null;

        public static HttpHelper _http = null;

        public static Account _account;

        public static HttpHelper HttpTransportation()
        {
            // Cache
            if (null == _http)
            {
                _http = new HttpHelper(Encoding.Default, Encoding.UTF8);
                // Login for the first time.

            }
            return _http;
        }


        public static void Login(Account account)
        {
            KXInterface._account = account;
            HttpHelper http = KXInterface.HttpTransportation();
            // login
            string url = string.Format(KXInterface.URL_LOGIN, account.Email, account.Pass);
            http.POST(url);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="appId">Different Application has Different Verifies.</param>
        /// <returns></returns>
        public static string ReadVerify(int appId)
        {
            HttpHelper http = KXInterface.HttpTransportation();
            // parking home
            string response = http.GET(string.Format(KXInterface.URL_KX_APP, appId), KXInterface.URL_KX_HOME);
            KXInterface._verify = ParkingAPI.ParseVerify(response);
            return KXInterface._verify;
        }
    }
}
